# _*_ codeing : utf-8 _*_
# @Time : 2025-04-09
# @Author : wxp
# @File : genModel
# @Project : wxp-python
import sys
sys.path.append("D:\\app\\Blender 4.4\\4.4\\scripts\\modules\\")
import bpy
import math
from mathutils import Vector


# 清理场景
def clear_scene():
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()


# 创建装甲车基础车身
def create_chassis():
    bpy.ops.mesh.primitive_cube_add(size=4, location=(0, 0, 2))
    chassis = bpy.context.object
    chassis.name = "ArmoredCar_Chassis"

    # 环切调整形状
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.loopcut_slide(
        MESH_OT_loopcut={"number_cuts": 2, "smoothness": 0},
        TRANSFORM_OT_edge_slide={"value": 0.5}
    )
    bpy.ops.object.mode_set(mode='OBJECT')

    # 添加装甲厚度
    chassis.modifiers.new("Armor", 'SOLIDIFY')
    chassis.modifiers["Armor"].thickness = 0.3
    bpy.ops.object.modifier_apply(modifier="Armor")
    return chassis


# 创建旋转炮塔
def create_turret():
    bpy.ops.mesh.primitive_cylinder_add(radius=1.5, depth=0.8, location=(0, 0, 3.5))
    turret = bpy.context.object
    turret.name = "Turret"

    # 创建炮管
    bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=5, location=(2.5, 0, 4))
    gun = bpy.context.object
    gun.name = "Gun"
    gun.rotation_euler = (0, math.radians(90), 0)

    # 父子关系
    gun.parent = turret
    return turret


# 创建带动画的轮子
def create_wheels(chassis):
    wheels = []
    positions = [(-3, -2, 1), (-3, 2, 1), (3, -2, 1), (3, 2, 1)]

    for loc in positions:
        bpy.ops.mesh.primitive_cylinder_add(radius=0.8, depth=0.5, location=loc)
        wheel = bpy.context.object
        wheel.name = "Wheel"
        wheel.rotation_euler = (math.radians(90), 0, 0)
        wheel.parent = chassis
        wheels.append(wheel)

    return wheels


# 绑定骨骼动画
def setup_armature(chassis, turret):
    # 创建骨骼
    bpy.ops.object.armature_add(location=chassis.location)
    armature = bpy.context.object
    armature.name = "Car_Armature"

    # 进入骨骼编辑模式
    bpy.context.view_layer.objects.active = armature
    bpy.ops.object.mode_set(mode='EDIT')

    # 创建根骨骼
    bone_root = armature.data.edit_bones.new("Root")
    bone_root.head = chassis.location
    bone_root.tail = chassis.location + Vector((0, 0, 2))

    # 创建炮塔骨骼
    bone_turret = armature.data.edit_bones.new("Turret_Joint")
    bone_turret.parent = bone_root
    bone_turret.head = turret.location
    bone_turret.tail = turret.location + Vector((0, 0, 1))

    # 返回对象模式
    bpy.ops.object.mode_set(mode='OBJECT')

    # 绑定模型到骨骼
    turret.parent = armature
    chassis.parent = armature

    return armature


# 创建动画关键帧
def create_animation(armature):
    # 初始化动画
    action = bpy.data.actions.new("ArmoredCar_Animation")
    armature.animation_data_create()
    armature.animation_data.action = action

    # 移动动画 (0-100帧)
    armature.location = (0, 0, 0)
    armature.keyframe_insert(data_path="location", frame=0)

    armature.location = (10, 0, 0)
    armature.keyframe_insert(data_path="location", frame=100)

    # 炮塔旋转动画 (Y轴)
    bone = armature.pose.bones["Turret_Joint"]
    bone.rotation_mode = 'XYZ'

    bone.rotation_euler.y = 0
    bone.keyframe_insert(data_path="rotation_euler", frame=0)

    bone.rotation_euler.y = math.radians(360)
    bone.keyframe_insert(data_path="rotation_euler", frame=100)


# 导出GLB文件
def export_glb(filepath):
    bpy.ops.export_scene.gltf(
        filepath=filepath,
        export_format='GLB',
        export_animations=True,
        export_apply=True,
        export_yup=True
    )


# 主流程
if __name__ == "__main__":
    clear_scene()

    # 创建模型
    chassis = create_chassis()
    turret = create_turret()
    wheels = create_wheels(chassis)

    # 绑定骨骼
    armature = setup_armature(chassis, turret)

    # 添加动画
    create_animation(armature)

    # 导出GLB
    export_glb("D:/test/3d-model/armored_car_animated.glb")  # 修改为你的输出路径

    print("装甲车GLB模型已生成!")
